<template>
  <div style="padding: 1vw">
    <div style="margin: 1vw;position: relative">
      <el-button v-if="this.formMsg.role==1" type="primary"  style="position: absolute;right: 5%" @click="add(spareId,sparePageId)">新增内容</el-button>
    </div>
    <div style="height: 2vw;margin: 1vw"/>
    <div>
      <el-row :gutter="0" >
        <el-col
            style="width: 100%"
            v-for="(item) in tableData"
            :key="item.id"
            :span ="8"
            :offset="0"
        >
          <el-card :body-style="{ padding: '0px' }"  style="box-shadow: none">
            <el-container style="text-align: center;" direction="vertical">
              <div style="margin-bottom: 1vw" v-if="this.formMsg.role==1">
                <el-button @click="handleEdit(item)" size="default">编辑</el-button>
                <el-popconfirm title="确定删除吗" @confirm="handDelete(item.id)" >
                  <template #reference><el-button size="default" >删除</el-button></template>
                </el-popconfirm>
              </div>
              <p style="text-align: center;" ><img w-full :src="item.avatarUrl" style="height: 30vw;width:70%;box-shadow: 0.1vw 0.1vw 0.1vw rgba(0,0,0,.5);"/></p>
              <el-header style="height: 4.5vw; align-content: center;font-size: 1.3vw;position: relative">
                <p style="text-align: center;position:absolute;top: 50%;left: 50%;transform: translate(-50%,-50%)">{{item.name}}</p>
              </el-header>
              <div style="position: relative" >
                <textarea class="my_textarea" disabled="true"
                >{{item.message}}</textarea>
              </div>
            </el-container>
          </el-card>
          <div style="height: 2.0vw;"></div>
        </el-col>
      </el-row>
    </div>
  </div>
  <el-dialog title="提示" v-model="dialogVisible" width="30%">
    <el-form :model="form" label-width="120px">
      <el-form-item label="姓名">
        <el-input v-model="form.name" style="width: 80%"></el-input>
      </el-form-item>
      <el-form-item label="描述">
        <el-input v-model="form.message" style="width: 80%"></el-input>
      </el-form-item>
      <el-form-item label="图片">
        <div style="border: 1px solid #8c939d;width:150px;height: 200px" >
          <el-upload
              ref="upload"
              action="http://localhost:9084/files/upload"
              :on-success="filesUploadSuccess"
              >
            <img v-if="form.avatarUrl" :src="form.avatarUrl" class="avatar" style="object-fit: fill;width: 150px;height: 200px"/>
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          </el-upload>
        </div>
      </el-form-item>
    </el-form>
    <template #footer>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </span>
    </template>
  </el-dialog>
</template>
<script>


import request from "@/utils/request";
import {calcTextareaHeight} from "element-plus/es/components/input/src/utils";

export default {
  name:'Component1',
  components:{
  },
  props:["spareId","sparePageId"],
  inject:['reload'],
  data(){
    return{
      form:{},
      dialogVisible:false,
      search:'',
      currentPage:1,
      pageSize:10,
      total:0,
      formMsg:{},
      tableData:[
      ]
    }
  },
  created() {
    let str=sessionStorage.getItem("user")||"{}"
    this.formMsg=JSON.parse(str);
    request.get("/user/"+this.formMsg.id).then(res=>{
      if(res.code==='0'){
        this.formMsg=res.data;
      }
    })
    this.load();
  },
  methods:{
    load(){
    request.get("/webpage1", {
      params:{
        searchId:this.spareId,
        searchPageId:this.sparePageId,
        kindId:1,
      }
    }).then(res=>{
      console.log(res);
      this.tableData=res;
    })
    },
    add(blockId,pageId){
      this.dialogVisible=true;
      this.form={};
      this.form.blockId=blockId;
      this.form.kindId=1;
      this.form.pageId=pageId;
      if(this.$refs['upload']){
        this.$refs['upload'].clearFiles();
      };
    },
    changeTitle(){
    },
    save(){
      if(this.form.id){
        request.put("/webpage1", this.form).then(res=>{
          console.log(res);
          if(res.code=='0'){
            this.$message({
              type:"success",
              message:"修改成功"
            })
          }else{
            this.$message({
              type:"error",
              message:res.msg
            })
          }
        })
      }
      else {
        request.post("/webpage1", this.form).then(res=>{
          console.log(res);
          this.$message({
            type:"success",
            message:"新增成功"
          })
        })
      }
      this.load();
      this.reload();
      this.dialogVisible=false;
    },
    handleEdit(id){
      this.form=JSON.parse(JSON.stringify(id));
      this.dialogVisible=true;
      this.$nextTick(()=>{
        this.$refs['upload'].clearFiles();
      });
    },
    handleSizeChange(pageSize){
      this.pageSize=pageSize;
      this.load();
    },
    handleCurrentChange(pageNum){
      this.pageNum=pageNum;
      this.load();
    },
    filesUploadSuccess(res){
      this.form.avatarUrl=res.data;
      console.log(res);
    },
    handDelete(id){
      console.log(id);
      request.delete("/webpage1/deletebyId/"+id).then(res=>{
        if(res.code=='0'){
          this.$message({
            type:"success",
            message:"删除成功"
          })
        }else{
          this.$message({
            type:"error",
            message:res.msg
          })
        }
        this.load();
        this.reload();
      })
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}
.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
.c-card{
  border: none;
  width: 393px;
  box-shadow: none;
}
.my_textarea{
  align-content: center;
  text-align: center;
  width: 240px;
  /*height: 100px;*/
  font-size: 16px;
  resize: none;
  border: none;
  outline: none;
  overflow-y: visible;

}
</style>